from scrapyProject.spiders.spiderBase import spiderBase
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import random
import time
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from lxml import etree
from scrapyProject.spiders.SeleniumCommon import SeleniumCommon

class qunar_spider(spiderBase, SeleniumCommon):
    name = 'qunar'
    #type=["Dynamic"]
    def start_requests(self):
        allowed_domains = ['qunar.com/']
        urls = ['https://flight.qunar.com//']
        urls = ["http://cs.sofang.com/new/area"]
        #urls = ['https://flight.qunar.com/touch/api/domestic/wbdflightlist?departureCity=%E6%AD%A6%E6%B1%89&arrivalCity=%E4%B8%8A%E6%B5%B7&departureDate=2019-01-26&ex_track=&__m__=3dfc65e3d7458f036606674d9c0e0736&sort=&_v=1']
        #urls=["https://flight.qunar.com/touch/api/domestic/wbdflightlist?departureCity=%E6%AD%A6%E6%B1%89&arrivalCity=%E4%B8%8A%E6%B5%B7&departureDate=2019-01-26"]
        #urls = ['https://flight.qunar.com/twell/flight/DynamicFlightInfo.jsp?departureCity=%E6%AD%A6%E6%B1%89&fromCity=%E6%AD%A6%E6%B1%89&arrivalCity=%E4%B8%8A%E6%B5%B7&toCity=%E4%B8%8A%E6%B5%B7&departureDate=2019-01-26&from=qunarindex&ex_track=&_v=1&isNewInterface=true']
        meta = {}
        meta["funcRequestHtmlData"] = "requestHtmlData"
        for url in urls:
            yield self.RequestUrl(url=url, callback=self.parse, isMustParse=True, meta=meta)

    def requestHtmlData(self, driver):
        print("页面开始渲染。。。")

        #'//*[@id="dfsForm"]//input[@name="fromCity"]'
        self.GetObjUntilVisual(driver, '//button[@type="submit" and contains(@data-track, "国内搜索")]')

        inFrom = self.GetObjUntilVisual(driver, '//*[@id="dfsForm"]//input[@name="fromCity"]')
        try:
            self.WaitObjUntilValueNotEmpty(inFrom)
        except TimeoutException:
            pass
        finally:
            inFrom.clear()

        inFrom.send_keys("北京")
        time.sleep(0.5+random.random()*0.5)

        all_options = self.GetObjUntilVisual(driver, '//*[@id="dfsForm"]//input[@name="fromCity"]/..//*[@class = "q-suggest"]//tr[@class="active"]')
        all_options.click()

        time.sleep(0.5+random.random()*0.5)
        inTo = self.GetObjUntilVisual(driver, '//*[@id="dfsForm"]//input[@name="toCity"]')
        inTo.clear()
        inTo.send_keys("上海")
        time.sleep(0.2+random.random()*0.5)

        all_options = self.GetObjUntilVisual(driver, '//*[@id="dfsForm"]//input[@name="toCity"]/..//*[@class = "q-suggest"]//tr[@class="active"]')
        all_options.click()

        time.sleep(0.8+random.random()*0.5)
        timeToGo = self.GetObjUntilVisual(driver, '//*[@id="fromDate"]')
        timeToGo.click()

        time.sleep(0.8+random.random()*0.5)


        self.GetObjUntilVisual(driver,  '(//*[@id="fromDate"]/..//*[@class="tdwrap"]//tr//td[@onclick])[1]')
        dateValidAll =  driver.find_elements_by_xpath( '//*[@id="fromDate"]/..//*[@class="tdwrap"]//tr//td[@onclick]')
        dateValidAll[10].click()


        cmdSure = self.GetObjUntilVisual(driver, '//button[@type="submit" and contains (@data-track, "国内搜索")]')
        time.sleep(1.0 + random.random())
        cmdSure.click()
        time.sleep(1.0+ random.random())
        self.WaitObjUntilExist(driver, '//*[@class="b-airfly"]')
        return  driver.page_source

    def parse(selfuncode):

        return

